using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using Rss;
using System.Runtime.Serialization;

namespace LWRSSReader
{
	[Serializable]
	[TypeConverter(typeof(RssFeedWrapTypeConverter))]
	public class RssFeedWrap : RssFeed, ISerializable
	{
		private RssFeed _feed;

		public RssFeedWrap()
		{
			_feed = new RssFeed();

		}

		public RssFeedWrap(string url)
		{
			this.Url = url;
		}


		private int _rssSelectedChannelId = -1;

		[Browsable(false)]
		public int RssSelectedChannelId
		{
			get { return _rssSelectedChannelId; }
		}



		private RssChannel _rssSelectedChannel;

		[TypeConverter(typeof(ChannelsTypeConverter)),
		DescriptionAttribute("The displayed channel of this feed"),
		DisplayName("Select channel to display")
		]
		public RssChannel RssSelectedChannel
		{
			get { return _rssSelectedChannel; }
			set
			{
				Object _tmp = value;
				_rssSelectedChannel = value;
				_rssSelectedChannelId = GetChannelId(_rssSelectedChannel);
			}
		}






		[Browsable(false)]
		public RssChannel this[string index]
		{
			get
			{
				if (Channels != null)
					foreach (RssChannel c in Channels)
					{
						if (c.Title == index)
						{
							return c;
						}
					}
				return null;
			}
		}

		public int GetChannelId(string channelTitle)
		{
			for (int i = 0; i < Channels.Count; i++)
			{
				if (Channels[i].Title == channelTitle)
				{
					return i;
				}
			}
			return -1;

		}
		public int GetChannelId(RssChannel channel)
		{
			for (int i = 0; i < Channels.Count; i++)
			{
				if (Channels[i].Equals(channel))
				{
					return i;
				}
			}
			return -1;
		}







		#region RssFeed almost copy-paste

		[Browsable(false)]
		public new RssChannelCollection Channels
		{
			get
			{

				if (_feed != null)
					return _feed.Channels;
				return null;

			}
		}

		[Browsable(false)]
		public new RssModuleCollection Modules
		{
			get { return _feed.Modules; }
		}

		[Browsable(false)]
		public new ExceptionCollection Exceptions
		{
			get { return _feed.Exceptions; }
		}

		[ReadOnly(true)]
		public new RssVersion Version
		{
			get
			{
				if (_feed != null)
					return _feed.Version;
				return RssVersion.Empty;
			}
		}

		[Browsable(false)]
		public new string ETag
		{
			get
			{
				if (_feed != null)
					return _feed.ETag;
				return null;
			}
		}

		[Browsable(false)]
		public new DateTime LastModified
		{
			get
			{
				if (_feed != null)
					return _feed.LastModified;
				return DateTime.MinValue;
			}
		}

		[Browsable(false)]
		public new bool Cached
		{
			get
			{
				if (_feed != null)
					return _feed.Cached;
				return false;
			}
		}

		public new string Url
		{
			get
			{
				if (_feed != null)
					return _feed.Url;
				return null;
			}
			set
			{
				_feed = RssFeed.Read(value);
			}
		}

		[Browsable(false)]
		public new Encoding Encoding
		{
			get { return _feed.Encoding; }
		}


		#endregion




		#region ISerializable Members

		void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
		{
			if (_feed == null)
				return;
			try
			{
				info.AddValue("feedUrl", _feed.Url);
				info.AddValue("feedChannelId", this._rssSelectedChannelId);
				if (this._rssSelectedChannel == null)
				{
					info.AddValue("feedChannelName", String.Empty);
				}
				else
				{
					info.AddValue("feedChannelName", this._rssSelectedChannel.Title);
				}
			}
			finally { }
		}


		public RssFeedWrap(SerializationInfo info, StreamingContext ctxt)
		{

			try
			{
				string fUrl = (string)info.GetValue("feedUrl", typeof(string));
				string fcName = (string)info.GetValue("feedChannelName", typeof(string));
				int fcId = (int)info.GetValue("feedChannelId", typeof(int));
				if (fUrl != String.Empty)
					_feed = RssFeed.Read(fUrl);
				else
					_feed = new RssFeed();
				if (this[fcName] != null)
				{
					this._rssSelectedChannel = this[fcName];

					for (int i = 0; i < _feed.Channels.Count; i++)
					{
						if (fcName == _feed.Channels[i].Title)
						{
							this._rssSelectedChannelId = i;
							break;
						}
					}
				}
				else if (fcId >= 0 && fcId < _feed.Channels.Count)
				{
					this._rssSelectedChannel = _feed.Channels[fcId];
					this._rssSelectedChannelId = fcId;

				}
			}

			finally { }




		}

		#endregion



	}
}
